﻿using Common.Attributes;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace QGZX.Logic.Zmjh
{
   public  class CourseSituationController : Controller
    {
       /// <summary>
       /// index视图
       /// </summary>
       /// <returns></returns>
       public ActionResult index()
       {
           return View();
       }

       #region 获取每一期课程的详细信息+GetCourseDetailData（）
       /// <summary>
       /// 获取每一期课程的详细信息+GetCourseDetailData（）
       /// </summary>
       /// <returns></returns>
       public ActionResult GetCourseDetailData()
       {
           //获取页容量
           int pageSize = int.Parse(Request.Form["rows"]);
           //获取请求页码
           int pageIndex = int.Parse(Request.Form["page"]);

           var list = OperateContext.Current.BLLSession.Iqgzx_zm_course_situationBLL.GetPagedList(pageIndex, pageSize, x => x.Id != null, m => m.Id);
           int rowCount = OperateContext.Current.BLLSession.Iqgzx_zm_course_situationBLL.GetListBy(x => x.Id != null).Count();

           MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
           {
               total = rowCount,
               rows = list,
               footer = null
           };
           return Json(dgMode);
       } 
       #endregion


       #region 加载修改竹铭课程 window 的html + EditCourseSituation()
       /// <summary>
       /// 加载修改竹铭课程 window 的html
       /// </summary>
       /// <returns></returns>
       [HttpGet]
       [AjaxRequest]
       public ActionResult EditCourseSituation(int id)
       {

           //根据id 查询要修改的权限
           SetDropDonwList();
           var model = OperateContext.Current.BLLSession.Iqgzx_zm_course_situationBLL.GetListBy(x => x.Id == id).FirstOrDefault().ToViewModel();
           //将权限对象 传给视图的model属性
           return PartialView(model);
       }
       #endregion

       #region post修改课程信息 EditCourseSituation(MODEL.qgzx_zm_course model)
       /// <summary>
       ///post修改课程详细情况 EditCourseSituation(MODEL.qgzx_zm_course_situation model)
       /// </summary>
       /// <returns></returns>
       [HttpPost]
       [AjaxRequest]
       public ActionResult EditCourseSituation(MODEL.qgzx_zm_course_situation model)
       {
           int res = OperateContext.Current.BLLSession.Iqgzx_zm_course_situationBLL.Modify(model, "Id", "Period", "CourseNum", "CourseId", "Teacher", "TeacherIntroduce",
               "CoursePlan", "ClassTime", "ClassLocation", "ClassNum", "ToBegin", "ToEnd",  "EnrollNum");
           if (res > 0)
           {
               return Redirect("/Zmjh/CourseSituation/index");
           }
           return Redirect("/Zmjh/CourseSituation/index");
       }
       #endregion

       #region 在这新增课程 html代码 + AddCourseSituation()
       /// <summary>
       /// 在这新增课程 html代码
       /// </summary>
       /// <returns></returns>
       [HttpGet]
       [AjaxRequest]
       public ActionResult AddCourseSituation()
       {

           SetDropDonwList();
           return PartialView("EditCourseSituation");
       }
       #endregion

       #region 在这新增课程 html代码 + AddCourseSituation(MODEL.qgzx_zm_course_situation model)
       /// <summary>
       /// 在这新增课程 html代码
       /// </summary>
       /// <returns></returns>
       [HttpPost]
       [AjaxRequest]
       public ActionResult AddCourseSituation(MODEL.qgzx_zm_course_situation model)
       {

           int res = OperateContext.Current.BLLSession.Iqgzx_zm_course_situationBLL.Add(model);

           if (res > 0)
           {
               return Redirect("/Zmjh/CourseSituation/index?ok");
           }
           return Redirect("/Zmjh/CourseSituation/index?err");
       }
       #endregion

       #region 删除课程 + DelCourseSituation()
       /// <summary>
       /// 删除课程 + DelCourse()
       /// </summary>
       /// <returns></returns>
       [HttpPost]
       [AjaxRequest]
       public ActionResult DelCourseSituation()
       {
           try
           {
               int id = int.Parse(Request.Form["id"]);
               OperateContext.Current.BLLSession.Iqgzx_zm_course_situationBLL.DelBy(x => x.Id == id);
               return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
           }
           catch (Exception ex)
           {

               return OperateContext.Current.RedirectAjax("err", "您现在删除的权限正在被使用,请先取消角色中的这个权限" + ex, null, "");
           }
       }
       #endregion

       #region 高级搜索(比较笨，先用这个方法) + ActionResult Search()
       /// <summary>
       /// 高级搜索(比较笨，先用这个方法)
       /// </summary>
       /// <returns></returns>
       [HttpPost]
       public ActionResult Search()
       {
           string Period = Request.QueryString["Period"];
           string CourseNum = Request.QueryString["CourseNum"];
          
           //获取页容量
           int pageSize = int.Parse(Request.Form["rows"]);
           //获取请求页码
           int pageIndex = int.Parse(Request.Form["page"]);
           //高级搜索的功能！！！
           var list = OperateContext.Current.BLLSession.Iqgzx_zm_course_situationBLL.GetListBy(x => x.Id != null).ToList();
           if (Period != "")
           {
               list = list.Where(x => x.Period.ToString().Contains(Period)).ToList();
           }
           if (CourseNum != "")
           {
               list = list.Where(x => x.CourseNum.Contains(CourseNum)).ToList();
           }
          
           int rowCount = list.Count();
           //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
           MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
           {
               total = rowCount,
               rows = list,
               footer = null
           };
           return Json(dgMode);
       }
       #endregion

       #region 设置新增和修改通用的下拉框数据 + SetDropDonwList()
       /// <summary>
       /// 设置新增和修改通用的下拉框数据
       /// </summary>
       void SetDropDonwList()
       {
           /*
            课程id对应课程
            */
           List<MODEL.qgzx_zm_course> list = OperateContext.Current.BLLSession.Iqgzx_zm_courseBLL.GetListBy(x => x.Id != null);
           List<SelectListItem> selectList = new List<SelectListItem>();
           foreach (MODEL.qgzx_zm_course model in list)
           {
               selectList.Add(new SelectListItem() { Text = model.CourseName, Value = model.Id.ToString() });
           }
           ViewBag.courselist = selectList;

           ///*
           // 性别
           // */
           //List<SelectListItem> sexList = new List<SelectListItem>() { 
           //    new SelectListItem() { Text = "男", Value = "1"},
           //    new SelectListItem() { Text = "女", Value = "2"}
           //};
           //ViewBag.sexList = sexList;
           ///*
           // 黑名单
           // */
           //List<SelectListItem> IsBlacklist = new List<SelectListItem>() { 
           //    new SelectListItem() { Text = "是", Value = "1"},
           //    new SelectListItem() { Text = "否", Value = "2"}
           //};
           //ViewBag.IsBlacklist = IsBlacklist;

       }
       #endregion
    }

}
